//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package fzdy.core.velocity;

import fzdy.core.common.SpringWebTool;
import org.apache.velocity.context.InternalContextAdapter;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.directive.Directive;
import org.apache.velocity.runtime.parser.node.Node;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.Writer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UrlFunction extends Directive {
    public UrlFunction() {
    }

    public String getName() {
        return "U";
    }

    public int getType() {
        return 2;
    }

    public boolean render(InternalContextAdapter internalContextAdapter, Writer writer, Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
        int i = node.jjtGetNumChildren();
        if(i > 0) {
            String urlStr = (String)node.jjtGetChild(0).value(internalContextAdapter);
            Object[] params = new Object[i - 1];

            for(int j = 1; j < i; ++j) {
                params[j - 1] = node.jjtGetChild(j).value(internalContextAdapter);
            }

            writer.write(buildUrl(urlStr, params));
        } else {
            writer.write(buildUrl("", new Object[0]));
        }

        return true;
    }

    public static String buildUrl(String urlStr, Object... param) {
        StringBuffer url = new StringBuffer();
        HttpServletRequest request = SpringWebTool.getRequest();
        String host = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
        url.append(host);
        urlStr = urlStr.trim();
        String requestURI = request.getRequestURI();
        if(urlStr == null || urlStr.length() == 0) {
            url.append(requestURI);
        } else {
            if(urlStr.charAt(0) != 47) {
                int i = requestURI.lastIndexOf("/");
                String substring = requestURI.substring(0, i);
                url.append(substring).append("/");
            } else {
                String contextPath = request.getContextPath();
                url.append(contextPath);
            }

            buildParam(urlStr, url, param);
        }

        return url.toString();
    }

    private static void buildParam(String urlStr, StringBuffer url, Object[] param) {
        Pattern pattern = Pattern.compile("\\?");
        Matcher matcher = pattern.matcher(urlStr);
        if(matcher.find()) {
            matcher.appendReplacement(url, ".do?");
            if(param != null) {
                for(int childIndex = 0; matcher.find() && childIndex < param.length; ++childIndex) {
                    if(param[childIndex] == null) {
                        matcher.appendReplacement(url, "");
                    } else {
                        matcher.appendReplacement(url, String.valueOf(param[childIndex]));
                    }
                }

                matcher.appendTail(url);
            }
        } else {
            url.append(urlStr).append(".do");
        }

    }
}
